<?php

namespace app\admin\controller;


use app\lib\enum\Status;
use think\Request;

class Article extends BaseController {
    //文章分类
    public function articleCategoryList() {
        if(!judgeRole(__METHOD__)){
            $this->redirect("admin/index/forbidden");
        }
        if (request()->isAjax()) {
            $categoryInfo=getSpecialOrderCategoryInfo();

            //再次编辑总数居，添加序号
            foreach ($categoryInfo as $key=>&$value){
                $value['index']=$key+1;
            }

            $count = count($categoryInfo);
            return getJson($count, $categoryInfo);
        }


        return view();
    }

    //添加类目
    public function articleCategoryAdd() {
        if (request()->isAjax()) {
            $data = Request::instance()->param();
            $this->uniteResponse(model("CmsArticleCategory")->articleCategoryAdd($data), "数据添加成功");
        }
        return view();
    }

    //编辑类目
    public function articleCategoryEdit() {
        $acc_id = input("get.acc_id");
        if (request()->isAjax()) {
            $data = Request::instance()->param();
            $this->uniteResponse(model("CmsArticleCategory")->articleCategoryEdit($data, $acc_id), "分类编辑成功");
        }
        $categoryInfo = model("CmsArticleCategory")->find($acc_id);
        $this->assign("categoryInfo", $categoryInfo);
        return view();
    }


    //文章列表
    public function articleList() {
        if(!judgeRole(__METHOD__)){
            $this->redirect("admin/index/forbidden");
        }
        if (request()->isAjax()) {
            $condition = [];
            $art_category_id = input("get.art_category_id");
            $art_title = input("get.art_title");
            $art_institution_id=input("get.art_institution_id");
            $art_ischeck=input("get.art_ischeck");
            $page = input("get.page");
            $limit = input("get.limit");

            if ($art_category_id != "" && $art_category_id != -1) {
                $condition['art_category_id'] = ['eq', $art_category_id];
            }
            if ($art_title != "") {
                $condition['art_title'] = ['like', "%" . $art_title . "%"];
            }
            if($art_institution_id!=""&&$art_institution_id!=-1){
                $condition['art_institution_id']=['eq',$art_institution_id];
            }
            if($art_ischeck!=""&&$art_ischeck!=-1){
                $condition['art_ischeck']=['eq',$art_ischeck];
            }
            if($data=isInstitutionAdmin()){
                $condition['art_institution_id']=['eq',$data["ins_id"]];
            }

            $count = count(model("CmsArticle")->select());
            $articleList = model("CmsArticle")->with("bindCategory,bindInstitution")->where($condition)
                ->order("art_id", "desc")
                ->page($page, $limit)
                ->select();
            foreach ($articleList as &$value) {
                $value['art_category']=$value['bind_category']['acc_name'];
                $value['art_create_time']=date("Y-m-d H:i:s",$value['art_create_time']);
                $value['art_keywords']=str_replace("|","，",$value['art_keywords']);
                $value['ins_name']=$value['bind_institution']['ins_name'];
                $value['art_keywords']=$value['art_keywords']!=""?$value['art_keywords']:"暂无数据";
            }
            return getJson($count, $articleList);
        }
        $categoryList = getSpecialOrderCategoryInfo(true);
        $this->assign("categoryList", $categoryList);
        $institutionData=getInstitutionListToArticle();
        $this->assign("institutionData",$institutionData);
        $this->assign("isInstitutionAdmin",isInstitutionAdmin());
        return view();
    }


    //好文列表
    public function goodArticleList(){
        if(!judgeRole(__METHOD__)){
            $this->redirect("admin/index/forbidden");
        }
        if (request()->isAjax()) {
            $condition = [];
            $condition["art_category_id"]=['in',getGoodArticleSubCategoryIds()];

            $art_category_id = input("get.art_category_id");
            $art_title = input("get.art_title");
            $art_institution_id=input("get.art_institution_id");
            $art_ischeck=input("get.art_ischeck");
            $page = input("get.page");
            $limit = input("get.limit");

            if ($art_category_id != "" && $art_category_id != -1) {
                $condition['art_category_id'] = ['eq', $art_category_id];
            }
            if ($art_title != "") {
                $condition['art_title'] = ['like', "%" . $art_title . "%"];
            }
            if($art_institution_id!=""&&$art_institution_id!=-1){
                $condition['art_institution_id']=['eq',$art_institution_id];
            }
            if($art_ischeck!=""&&$art_ischeck!=-1){
                $condition['art_ischeck']=['eq',$art_ischeck];
            }
            if($data=isInstitutionAdmin()){
                $condition['art_institution_id']=['eq',$data["ins_id"]];
            }

            $countData =model("CmsArticle")->field("count(*)")->where($condition)->select();
            $count=$countData[0]['count(*)'];
            $articleList = model("CmsArticle")->with("bindCategory,bindInstitution")->where($condition)
                ->order("art_id", "desc")
                ->page($page, $limit)
                ->select();
            foreach ($articleList as &$value) {
                $value['art_category']=$value['bind_category']['acc_name'];
                $value['art_create_time']=date("Y-m-d H:i:s",$value['art_create_time']);
                $value['art_keywords']=str_replace("|","，",$value['art_keywords']);
                $value['ins_name']=$value['bind_institution']['ins_name'];
                $value['art_keywords']=$value['art_keywords']!=""?$value['art_keywords']:"暂无数据";
                $value['art_expire_time']=date("Y-m-d",$value['art_expire_time']);
            }
            return getJson($count, $articleList);
        }
        $categoryList =getGoodArticleSubCategoryList();
        $this->assign("categoryList", $categoryList);
        $institutionData=getInstitutionListToArticle();
        $this->assign("institutionData",$institutionData);
        $this->assign("isInstitutionAdmin",isInstitutionAdmin());
        return view();
    }

    //好课列表
    public function goodClassList(){
        if(!judgeRole(__METHOD__)){
            $this->redirect("admin/index/forbidden");
        }
        if (request()->isAjax()) {
            $condition = [];
            $condition["art_category_id"]=['in',getGoodClassSubCategoryIds()];
            $art_category_id = input("get.art_category_id");
            $art_title = input("get.art_title");
            $art_institution_id=input("get.art_institution_id");
            $art_ischeck=input("get.art_ischeck");
            $page = input("get.page");
            $limit = input("get.limit");

            if ($art_category_id != "" && $art_category_id != -1) {
                $condition['art_category_id'] = ['eq', $art_category_id];
            }
            if ($art_title != "") {
                $condition['art_title'] = ['like', "%" . $art_title . "%"];
            }
            if($art_institution_id!=""&&$art_institution_id!=-1){
                $condition['art_institution_id']=['eq',$art_institution_id];
            }
            if($art_ischeck!=""&&$art_ischeck!=-1){
                $condition['art_ischeck']=['eq',$art_ischeck];
            }
            if($data=isInstitutionAdmin()){
                $condition['art_institution_id']=['eq',$data["ins_id"]];
            }


            $countData =model("CmsArticle")->field("count(*)")->where($condition)->select();
            $count=$countData[0]['count(*)'];
            $articleList = model("CmsArticle")->with("bindCategory,bindInstitution")->where($condition)
                ->order("art_id", "desc")
                ->page($page, $limit)
                ->select();
            foreach ($articleList as &$value) {
                $value['art_category']=$value['bind_category']['acc_name'];
                $value['art_create_time']=date("Y-m-d H:i:s",$value['art_create_time']);
                $value['art_keywords']=str_replace("|","，",$value['art_keywords']);
                $value['ins_name']=$value['bind_institution']['ins_name'];
                $value['art_keywords']=$value['art_keywords']!=""?$value['art_keywords']:"暂无数据";
                $value['art_expire_time']=date("Y-m-d",$value['art_expire_time']);
            }
            return getJson($count, $articleList);
        }
        $categoryList = getGoodClassSubCategoryList();
        $this->assign("categoryList", $categoryList);
        $institutionData=getInstitutionListToArticle();
        $this->assign("institutionData",$institutionData);
        $this->assign("isInstitutionAdmin",isInstitutionAdmin());
        return view();
    }



    //文章添加
    //必填：标题，排序，分类
    public function articleAdd(){
        if(request()->isAjax()){
            $data=handleArticleData(Request::instance()->param());
            $this->uniteResponse(model("CmsArticle")->articleAdd($data),"文章添加成功");
        }

        $option=input("get.option");
        $isGoodClass=true;  //当前请求是否为好课请求
        if($option=="goodArticle"){
            $parentCategoryInfo=getGoodArticleCategoryInfo();
            $isGoodClass=false;
        }else{
            $parentCategoryInfo=getGoodClassCategoryInfo();
        }
        $this->assign("parentCategoryInfo",$parentCategoryInfo);
        $this->assign("isGoodClass",$isGoodClass);

        //获取培训机构信息
        $institutionData=getInstitutionListToArticle();
        $this->assign("institutionData",$institutionData);
        $this->assign("isInstitutionAdmin",isInstitutionAdmin());
        return view();
    }


    //文章编辑
    public function articleEdit(){
        $art_id=input("get.art_id");
        if(!checkArticle($art_id)){
            $this->error("无权限修改该文章信息");
        }

        if(request()->isAjax()){
            //只有系统管理员，才能修改文章的状态
            $data=handleArticleData(Request::instance()->param());
            $this->uniteResponse(model("CmsArticle")->articleEdit($data,$art_id),"文章编辑成功");
        }


        $articleInfo=model("CmsArticle")->find($art_id);
        $articleInfo['art_expire_time']=date("Y-m-d",$articleInfo['art_expire_time']);

        //文章的分类
        $acc_id=$articleInfo['art_category_id'];
        $categoryInfo=model("CmsArticleCategory")->find($acc_id);
        $option=input("get.option");
        $isGoodClass=true;
        if($option=="goodArticle"){
            $parentCategoryInfo=getGoodArticleCategoryInfo();
            $subCategoryInfo=getGoodArticleSubCategoryList();
            $isGoodClass=false;
        }else{
            $parentCategoryInfo=getGoodClassCategoryInfo();
            $subCategoryInfo=getGoodClassSubCategoryList();
        }
        $this->assign("parentCategoryInfo",$parentCategoryInfo);
        $this->assign("subCategoryInfo",$subCategoryInfo);
        $this->assign("isGoodClass",$isGoodClass);


        $this->assign("acc_id",$acc_id);    //当前文章的分类id
        $this->assign("articleInfo",$articleInfo);  //文章信息
        //判断文章的过期时间是否为"永不过期"
        $neverExpire=false;
        if($articleInfo['art_expire_time']==config("setting.neverExpireTime")){
            $neverExpire=true;
        }
        $this->assign("neverExpire",$neverExpire);


        //文章的所属机构
        $institutionData=getInstitutionListToArticle();
        $this->assign("institutionData",$institutionData);


        //是否是机构管理员操作
        $this->assign("isInstitutionAdmin",isInstitutionAdmin());



        return view();
    }


    //根据父级分类，返回子分类
    public function getChildCategoryInfo(){
        $parent_id=input("post.parent_id");
        $childCategoryInfo=model("CmsArticleCategory")->where("acc_parent_id","=",$parent_id)->select();
        $returnData="";
        $msg="noData";
        if($childCategoryInfo){
            $msg="success";
            foreach ($childCategoryInfo as $value){
                $tempData="<option value='%s'>%s</option>";
                $tempData=sprintf($tempData,$value['acc_id'],$value['acc_name']);
                $returnData.=$tempData;
            }

        }
        $this->success($msg,"",$returnData);
    }



    //文章删除
    public function articleDel(){
        $art_id=input("post.art_id");
        if(!checkArticle($art_id)){
            $this->error("无权限删除该文章");
        }
        $result=model("CmsArticle")->where("art_id","=",$art_id)->delete();
        if(!$result){
            $this->error("文章删除失败");
        }else{
            $this->success("文章删除成功");
        }
    }



    //处理图片上传
    public function uploadImg(){
        $file = request()->file('file');
        $src=imgUpload($file,false);
        $result=[
            "code"=>0,
            "mag"=>"上传成功",
            "data"=>[
                "src"=>$src,
            ]
        ];
        return json($result);
    }
}
